﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI.DataVisualization.Charting;

namespace TestOnlineProject.Common
{
    public class ChartHelper
    {
        private int countValue = 0;
        public Title CreateTitle(string chartType)
        {
            var title = new Title
                              {
                                  ShadowColor = Color.FromArgb(32, 0, 0, 0),
                                  Font = new Font("Trebuchet MS", 14F, FontStyle.Bold),
                                  ShadowOffset = 3,
                                  ForeColor = Color.FromArgb(26, 59, 105)
                              };
            // Title for chart of test
            if (chartType.Equals("TestChart"))
            {
                title.Text = "Users per mark (%)";
            }
            // title for pie chart of question
            else if (chartType.Equals("QuestionPieChart"))
            {
                title.Text = "Users per answer(%)";
            }
            // title for column chart of question
            else
            {
                title.Text = "Users per answer";
            }

            return title;
        }

        public Series CreateSeries(int[] yValue, string chartType, List<string> xValue)
        {
            Series seriesDetail = null;
            // Series for chart of Test
            if (chartType.Equals("TestChart"))
            {
                int sum = 0;
                // Style of series
                seriesDetail = new Series
                {
                    Name = "Mark Chart",
                    IsValueShownAsLabel = false,
                    Color = Color.FromArgb(64, 54, 245),
                    ChartType = SeriesChartType.Column,
                    BorderWidth = 2
                };
                // get total users
                for (int i = 0; i < yValue.Count(); i++)
                {
                    sum += yValue[i];
                }
                for (int i = 0; i < yValue.Count(); i++)
                {
                    if (sum > 0)
                    {
                        // add to value point
                        var point = new DataPoint
                        {
                            YValues = new double[] {Math.Round((double)(yValue[i] * 100 / sum)) },
                            AxisLabel = i.ToString(),
                        };
                        // show label
                        if (yValue[i] != 0)
                        {
                            countValue++;
                            point.IsValueShownAsLabel = true;
                        }
                        // add point to series
                        seriesDetail.Points.Add(point);
                    }
                }
                seriesDetail.ChartArea = "Mark Chart";
            }
            else if (chartType.Equals("QuestionColumnChart"))
            {
                // Style of series
                seriesDetail = new Series
                {
                    Name = "Answer Chart",
                    IsValueShownAsLabel = false,
                    Color = Color.FromArgb(64, 54, 245),
                    ChartType = SeriesChartType.Column,
                    BorderWidth = 2
                };
                for (int i = 0; i < yValue.Count(); i++)
                {
                    // Add value to point
                    var point = new DataPoint
                    {
                        YValues = new double[] { yValue[i] },
                        AxisLabel = xValue[i],
                    };
                    // Show label
                    if (yValue[i] != 0)
                    {
                        point.IsValueShownAsLabel = true;
                    }
                    // Add point to series
                    seriesDetail.Points.Add(point);
                }
                seriesDetail.ChartArea = "Answer Chart";
            }
                // Series for pie chart of question
            else if (chartType.Equals("QuestionPieChart"))
            {
                // Style of series
                seriesDetail = new Series
                {
                    Name = "Answer Chart",
                    IsValueShownAsLabel = false,
                    ChartType = SeriesChartType.Pie,
                    BorderWidth = 2,
                };
                for (int i = 0; i < yValue.Count(); i++)
                {
                    // Add value to point
                    var point = new DataPoint
                    {
                        YValues = new double[] { yValue[i] },
                        AxisLabel = xValue[i]
                    };
                    point.IsValueShownAsLabel = true;
                    // Add point to series
                    seriesDetail.Points.Add(point);
                }
                seriesDetail.ChartArea = "Answer Chart";
            }
                // Series for column chart of question
            else
            {
                seriesDetail = new Series
                {
                    IsValueShownAsLabel = false,
                    ChartType = SeriesChartType.StackedColumn100,
                    BorderWidth = 2,
                };
                for (int i = 0; i < yValue.Count(); i++)
                {
                    var point = new DataPoint
                                    {
                                        YValues = new double[] { yValue[i] },
                                    };
                    if(i<xValue.Count())
                    {
                        point.AxisLabel = xValue[i];
                    }
                    // Show label
                    if (yValue[i] != 0)
                    {
                        point.IsValueShownAsLabel = true;
                    }
                    seriesDetail.Points.Add(point);
                }
                seriesDetail.ChartArea = "Answer Chart";
            }
            return seriesDetail;
        }
        public Legend CreateLengend()
        {
            var legend = new Legend();
            legend.BackColor = Color.FromArgb(222, 254, 253);
            return legend;
        }
        public ChartArea CreateChartArea(string chartType)
        {
            ChartArea chartArea = null;
            // ChartArea for chart of test
            if (chartType.Equals("TestChart"))
            {
                chartArea = new ChartArea
                {
                    Name = "Mark Chart",
                    BackColor = Color.Transparent,
                    AxisX = { IsLabelAutoFit = false },
                    AxisY = { IsLabelAutoFit = false }
                };
                chartArea.AxisX.LabelStyle.Font =
                   new Font("Verdana,Arial,Helvetica,sans-serif",
                            8F, FontStyle.Regular);
                chartArea.AxisY.LabelStyle.Font =
                   new Font("Verdana,Arial,Helvetica,sans-serif",
                            8F, FontStyle.Regular);
                chartArea.AxisY.LineColor = Color.FromArgb(64, 64, 64, 64);
                chartArea.AxisX.LineColor = Color.FromArgb(64, 64, 64, 64);
                chartArea.AxisY.MajorGrid.LineColor = Color.FromArgb(64, 64, 64, 64);
                chartArea.AxisX.MajorGrid.LineColor = Color.FromArgb(64, 64, 64, 64);
                chartArea.AxisX.Interval = 1;
                if(countValue==1)
                {
                    chartArea.AxisY.Maximum = 100;
                }
                chartArea.AxisX.Title = "Mark";
                chartArea.AxisY.Title = "Rate of user";
            }
                // ChartArea for pie chart of question
            else if (chartType.Equals("QuestionPieChart"))
            {
                chartArea = new ChartArea
                {
                    Name = "Answer Chart",
                    BackColor = Color.Transparent,
                    AxisX = { IsLabelAutoFit = false },
                    AxisY = { IsLabelAutoFit = true }
                };
                chartArea.AxisX.LabelStyle.Font =
                   new Font("Verdana,Arial,Helvetica,sans-serif",
                            8F, FontStyle.Regular);
                chartArea.AxisY.LabelStyle.Font =
                   new Font("Verdana,Arial,Helvetica,sans-serif",
                            8F, FontStyle.Regular);
            }
                // ChartArea for column chart of question
            else
            {
                chartArea = new ChartArea
                {
                    Name = "Answer Chart",
                    BackColor = Color.Transparent,
                    AxisX = { IsLabelAutoFit = true },
                    AxisY = { IsLabelAutoFit = true }
                };
                chartArea.AxisX.LabelStyle.Font =
                   new Font("Verdana,Arial,Helvetica,sans-serif",
                            8F, FontStyle.Regular);
                chartArea.AxisY.LabelStyle.Font =
                   new Font("Verdana,Arial,Helvetica,sans-serif",
                            8F, FontStyle.Regular);
                chartArea.AxisY.LineColor = Color.FromArgb(64, 64, 64, 64);
                chartArea.AxisX.LineColor = Color.FromArgb(64, 64, 64, 64);
                chartArea.AxisY.MajorGrid.LineColor = Color.FromArgb(64, 64, 64, 64);
                chartArea.AxisX.MajorGrid.LineColor = Color.FromArgb(64, 64, 64, 64);
                chartArea.AxisX.Interval = 1;
                if (chartType.Equals("QuestionColumnChart"))
                {
                    chartArea.AxisY.Title = "Number of user";

                }
                else
                {
                    chartArea.AxisY.Title = "Rate of user";
                }    
            } 
            return chartArea;
        }
    }
}